<template>
    <div class="doctor-detail-page">
        <!-- 顶部导航 -->
        <page-header :title="doctor.name" />

        <div class="page-content">
            <!-- 医生基本信息 -->
            <div class="doctor-info">
                <div class="basic-info">
                    <img :src="doctor.avatar || '/images/doctor-default.jpg'" class="doctor-avatar" />
                    <div class="info-content">
                        <div class="doctor-name">{{ doctor.name }}</div>
                        <div class="doctor-title">{{ doctor.title }} | {{ doctor.department }}</div>
                        <div class="doctor-tags">
                            <span class="tag">{{ doctor.specialty }}</span>
                            <span class="tag">{{ doctor.experience }}年经验</span>
                        </div>
                    </div>
                </div>

                <div class="speciality-info">
                    <div class="speciality-title">擅长</div>
                    <div class="speciality-content">{{ doctor.specialities }}</div>
                </div>
            </div>

            <!-- 出诊安排 -->
            <div class="schedule-info">
                <div class="section-title">出诊安排</div>

                <div class="date-selector">
                    <div v-for="(date, index) in scheduleDates" :key="index" class="date-item"
                        :class="{ active: activeDate === index }" @click="activeDate = index">
                        <div class="day">{{ date.day }}</div>
                        <div class="date">{{ date.date }}</div>
                        <div class="status" :class="{ available: date.available }">
                            {{ date.available ? '有号' : '约满' }}
                        </div>
                    </div>
                </div>

                <div class="time-slots">
                    <div class="time-period">
                        <div class="period-title">上午</div>
                        <div class="slots">
                            <div v-for="(slot, index) in morningSlots" :key="index" class="time-slot"
                                :class="{ disabled: !slot.available }" @click="slot.available && selectTimeSlot(slot)">
                                <div class="time">{{ slot.time }}</div>
                                <div class="fee">{{ slot.fee }}元</div>
                                <div class="remaining">{{ slot.available ? `剩${slot.remaining}个` : '已约满' }}</div>
                            </div>
                        </div>
                    </div>

                    <div class="time-period">
                        <div class="period-title">下午</div>
                        <div class="slots">
                            <div v-for="(slot, index) in afternoonSlots" :key="index" class="time-slot"
                                :class="{ disabled: !slot.available }" @click="slot.available && selectTimeSlot(slot)">
                                <div class="time">{{ slot.time }}</div>
                                <div class="fee">{{ slot.fee }}元</div>
                                <div class="remaining">{{ slot.available ? `剩${slot.remaining}个` : '已约满' }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 个人简介 -->
            <div class="doctor-profile">
                <div class="section-title">个人简介</div>
                <div class="profile-content">{{ doctor.profile }}</div>
            </div>
        </div>

        <!-- 底部预约按钮 -->
        <div class="bottom-buttons">
            <van-button type="primary" block @click="onBooking">立即预约</van-button>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { showToast } from 'vant'
import PageHeader from '@/components/common/PageHeader.vue'

const route = useRoute()
const router = useRouter()
const hospitalId = Number(route.params.id)
const doctorId = Number(route.params.doctorId)

// 模拟医生数据
const doctor = ref({
    id: doctorId,
    name: '张医生',
    avatar: '/images/doctor1.jpg',
    title: '主任医师',
    department: '心内科',
    specialty: '心血管疾病',
    experience: 15,
    specialities: '冠心病、心肌梗死、高血压、心律失常、心力衰竭等心血管疾病的诊断和治疗。',
    profile: '张医生，主任医师，教授，博士生导师，从事心血管内科临床工作20余年，曾在美国哈佛大学医学院附属麻省总医院心脏中心进修。擅长冠心病、心肌梗死、高血压、心律失常、心力衰竭等心血管疾病的诊断和治疗。发表学术论文100余篇，主持国家及省部级科研课题多项。'
})

// 选择的日期索引
const activeDate = ref(0)

// 模拟日期数据
const scheduleDates = ref([
    { day: '今天', date: '5/20', available: true },
    { day: '明天', date: '5/21', available: true },
    { day: '周五', date: '5/22', available: true },
    { day: '周六', date: '5/23', available: false },
    { day: '周日', date: '5/24', available: true },
    { day: '周一', date: '5/25', available: true },
    { day: '周二', date: '5/26', available: true }
])

// 模拟上午时间段
const morningSlots = ref([
    { time: '08:00', fee: 30, available: true, remaining: 5 },
    { time: '09:00', fee: 30, available: true, remaining: 2 },
    { time: '10:00', fee: 30, available: false, remaining: 0 },
    { time: '11:00', fee: 30, available: true, remaining: 8 }
])

// 模拟下午时间段
const afternoonSlots = ref([
    { time: '13:30', fee: 30, available: true, remaining: 10 },
    { time: '14:30', fee: 30, available: true, remaining: 7 },
    { time: '15:30', fee: 30, available: false, remaining: 0 }
])

// 选择时间段
const selectTimeSlot = (slot: any) => {
    const date = scheduleDates.value[activeDate.value]
    showToast(`已选择: ${date.date} ${slot.time}`)
    // 在实际应用中，应该存储选中的时间段信息
}

// 立即预约
const onBooking = () => {
    router.push(`/hospital/${hospitalId}/booking?doctorId=${doctorId}`)
}

onMounted(() => {
    // 在实际应用中，这里应该根据 hospitalId 和 doctorId 从API获取医生详情
    console.log(`加载医院ID: ${hospitalId}, 医生ID: ${doctorId}`)
})
</script>

<style scoped lang="scss">
.doctor-detail-page {
    min-height: 100vh;
    background: #f5f5f5;
    padding-top: 46px; // 为顶部导航栏预留空间
    padding-bottom: 60px; // 为底部按钮预留空间

    .page-content {
        padding-bottom: 16px;
    }

    .doctor-info {
        background: #fff;
        padding: 16px;
        margin-bottom: 8px;

        .basic-info {
            display: flex;
            margin-bottom: 16px;

            .doctor-avatar {
                width: 70px;
                height: 70px;
                border-radius: 50%;
                margin-right: 12px;
            }

            .info-content {
                flex: 1;

                .doctor-name {
                    font-size: 18px;
                    font-weight: 500;
                    color: #333;
                    margin-bottom: 6px;
                }

                .doctor-title {
                    font-size: 14px;
                    color: #666;
                    margin-bottom: 8px;
                }

                .doctor-tags {
                    .tag {
                        display: inline-block;
                        background: #f5f5f5;
                        padding: 2px 8px;
                        border-radius: 4px;
                        font-size: 12px;
                        color: #666;
                        margin-right: 8px;
                    }
                }
            }
        }

        .speciality-info {
            .speciality-title {
                font-size: 14px;
                font-weight: 500;
                color: #333;
                margin-bottom: 8px;
            }

            .speciality-content {
                font-size: 14px;
                color: #666;
                line-height: 1.6;
            }
        }
    }

    .schedule-info,
    .doctor-profile {
        background: #fff;
        padding: 16px;
        margin-bottom: 8px;

        .section-title {
            font-size: 16px;
            font-weight: 500;
            color: #333;
            margin-bottom: 16px;
        }
    }

    .date-selector {
        display: flex;
        overflow-x: auto;
        margin-bottom: 16px;

        &::-webkit-scrollbar {
            display: none;
        }

        .date-item {
            flex: 0 0 70px;
            text-align: center;
            padding: 8px 0;
            border-radius: 4px;
            margin-right: 8px;
            background: #f5f5f5;

            &.active {
                background: #E6F7FF;

                .day,
                .date {
                    color: #1890FF;
                }
            }

            .day {
                font-size: 14px;
                color: #333;
                margin-bottom: 4px;
            }

            .date {
                font-size: 12px;
                color: #666;
                margin-bottom: 4px;
            }

            .status {
                font-size: 12px;
                color: #999;

                &.available {
                    color: #52C41A;
                }
            }
        }
    }

    .time-slots {
        .time-period {
            margin-bottom: 16px;

            .period-title {
                font-size: 14px;
                font-weight: 500;
                color: #333;
                margin-bottom: 12px;
            }

            .slots {
                display: flex;
                flex-wrap: wrap;

                .time-slot {
                    width: calc(50% - 8px);
                    height: 80px;
                    background: #f9f9f9;
                    border-radius: 4px;
                    padding: 12px;
                    margin-right: 8px;
                    margin-bottom: 8px;
                    display: flex;
                    flex-direction: column;
                    justify-content: center;
                    position: relative;

                    &:nth-child(2n) {
                        margin-right: 0;
                    }

                    &.disabled {
                        opacity: 0.5;
                    }

                    .time {
                        font-size: 16px;
                        font-weight: 500;
                        color: #333;
                        margin-bottom: 4px;
                    }

                    .fee {
                        font-size: 14px;
                        color: #FF525D;
                        margin-bottom: 4px;
                    }

                    .remaining {
                        font-size: 12px;
                        color: #999;
                    }
                }
            }
        }
    }

    .doctor-profile {
        .profile-content {
            font-size: 14px;
            color: #666;
            line-height: 1.6;
        }
    }

    .bottom-buttons {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 8px 16px;
        background: #fff;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);

        .van-button {
            height: 44px;
            border-radius: 22px;
        }
    }
}
</style>